<template>
  <div class="fullscreen">
    <div class="loginBox">
      <div class="login-content">
        <div class="left" />
        <div class="right">
          <div v-loading="isLoging" class="rightCon">
            <p class="desc mt10">欢迎登录用户管理中心</p>
            <div class="itemInput">
              <i class="icon icons icon-user setPadding" />
              <el-input
                v-model="loginModel.userPhoneNum"
                placeholder="请输入用户名"
                class="myInput"
                @keyup.enter.native="getGoLogin"
              />
            </div>
            <div class="itemInput">
              <i class="icon icons icon-pass setPadding" />
              <el-input
                v-model="loginModel.pwd"
                placeholder="请输入密码"
                class="myInput"
                type="password"
                show-password
                @keyup.enter.native="getGoLogin"
              />
            </div>
            <div class="codeBox">
              <div class="itemInput codeLeft">
                <i class="icon icons icon-code setPadding" />
                <el-input
                  v-model="loginModel.verifyCode"
                  placeholder="请输入验证码"
                  class="myInput"
                  @keyup.enter.native="getGoLogin"
                />
              </div>
              <el-image
                class="codeImg"
                :src="validImg"
                title="点击更换"
                fit="scale-down"
                @click="getCreateCodeValid(true)"
              />
            </div>
            <el-button
              type="primary"
              class="loginBtn mt20"
              :disabled="
                !loginModel.userPhoneNum ||
                  !loginModel.pwd ||
                  !loginModel.verifyCode
                  ? true
                  : false
              "
              @click="getGoLogin"
            >登 录</el-button>
            <div class="forget-password">
              忘记密码 >>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { mapActions, mapState } from 'vuex'
import { setToken, removeToken, setServerTime } from '@/common/auth'
import Sever from '@/api/selfApi'
import qs from 'qs'
import md5 from 'js-md5' // md5

export default {
  data() {
    return {
      isLoging: false,
      loginModel: {
        userPhoneNum: '',
        pwd: '',
        deviceType: -1,
        verifyCode: '',
        appId: 'base' // 登录时获取菜单用，有登录系统提供
      },
      validImg: '',
      time: '',
      loginTime: 0,
      timeOut: ''
    }
  },
  computed: {},
  mounted() {
    this.getCreateCodeValid()
    // this.getServerTimeLogin()
  },
  beforeDestroy() {
    clearTimeout(this.timeOut)
  },
  methods: {
    ...mapActions(['logout', '_getServerTime']),
    async getServerTimeLogin() {
      const date = new Date()
      const res = await Sever.login.getServerTime()
      this.loginTime = date.getTime() - res
      console.log(this.loginTime)
      setServerTime(this.loginTime)
    },
    async getCreateCodeValid() {
      const res = await Sever.login.vertifyCode()
      this.validImg = window.URL.createObjectURL(res.data)
      // 由于验证码图片有效期为3分钟，一旦页面超过三分钟，验证码过期，
      // 就会导致提交时提示验证码错误，所以加个定时器，每3分钟刷新一次验证码图片
      this.timeOut = setTimeout(() => {
        this.getCreateCodeValid()
      }, 170000)
    },
    async getGoLogin() {
      this.$router.push({ name: 'demohome' })
      removeToken()
      if (!this.loginModel.userPhoneNum) return this.$hint.hint('请输入账号！')
      if (!this.loginModel.pwd) return this.$hint.hint('请输入密码！')
      if (!this.loginModel.verifyCode) return this.$hint.hint('请输入验证码！')
      const pwd = md5(this.loginModel.pwd)
      const result = await Sever.login.userLogin({
        ...this.loginModel,
        pwd
      })
      if (result) {
        this.$store.commit('MENUS', result.ccompanyMenuList)
        setToken(result.userToken)
        this.$store.commit('GET_LOGIN_DATA', result)
        await this._getServerTime()
        this.$hint.succ('登录成功！')
        this.$router.push({ name: 'adminMain' })
      } else {
        this.$hint.erro(message)
      }
    }
  }
}
</script>
<style lang="scss">
.myInput input {
  border: 0 !important;
  font-size: $ft16;
}
</style>
<style lang="scss" scoped>
.fullscreen {
  position: absolute;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .loginBox {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
  }
  .login-content {
    flex: 1;
    background: #fff;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: url('../assets/img/loginBg.png') no-repeat;
    background-size: 100% 100%;
    .left {
      flex: 1;
      display: flex;
      align-items: center;
      img {
        width: 100%;
        min-width: 600px;
        max-width: 1100px;
      }
    }
    .right {
      background-image: url('../assets/img/login-form-bg.png');
      width: 460px;
      height: 500px;
      margin-right: 70px;
      .rightCon {
        width: 100%;
        padding: 40px 20px;
        h2 {
          text-align: left;
          font-size: $ft24;
          font-weight: bold;
          margin: 0;
          span {
            font-size: $ft14;
          }
        }
      }
      ::v-deep .el-input__inner {
        background-color: rgba(0, 0, 0, 0);
        color: #fff;
      }
    }
  }
  .desc {
    text-align: center;
    font-weight: 700;
    font-size: 30px;
    font-family: Adobe Heiti Std;
    font-weight: 400;
    color: #0eedf1;
    line-height: 28px;
  }
  .itemInput {
    display: flex;
    align-items: center;
    height: 50px;
    border-bottom: 1px solid #0beaeb;
    margin-top: 45px;
    i {
      color: #0beaeb;
      font-size: $ft20;
    }
  }
  .loginBtn {
    width: 100%;
    height: 45px;
    background-color: #0beaeb;
  }
  .codeBox {
    display: flex;
    margin-top: 45px;
    align-items: center;
    .codeImg {
      width: 135px;
      height: 50px;
      cursor: pointer;
      margin-left: 20px;
    }
    .itemInput {
      margin-top: 0;
    }
  }
  .setPadding {
    padding: 0 25px;
  }
  .forget-password {
    margin-top: 15px;
    text-align: right;
    color: #409eff;
    text-decoration: #fff;
    cursor: pointer;
    padding-right: 10px;
    font-size: 16px;
  }
}
</style>
